package com.choupin.choupin.utils.images;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;

public class ImagesUtils {
    /**
     * 保存图像为多种文件格式
     * @param image 要保存的图像对象
     * @param fileName 保存的文件名（不包括扩展名）
     * @param formats 要保存的文件格式列表
     * @throws IOException 如果保存过程中发生错误
     */
    public static void saveImageAsMultipleFormats(BufferedImage image, String fileName, List<String> formats) throws IOException {
        for (String format : formats) {
            // 使用ImageIO.write()方法将图像保存为指定格式
            ImageIO.write(image, format, new File(fileName + "." + format));
        }
    }
    public static void copyImage(String sourcePath, String targetPath) throws IOException {
        File sourceFile = new File(sourcePath);
        File targetFile = new File(targetPath);

        if (!sourceFile.exists()) {
            throw new IllegalArgumentException("源文件不存在");
        }

        if (targetFile.exists()) {
            throw new IllegalArgumentException("目标文件已存在");
        }

        try (FileInputStream fis = new FileInputStream(sourceFile);
             FileOutputStream fos = new FileOutputStream(targetFile);
             FileChannel sourceChannel = fis.getChannel();
             FileChannel targetChannel = fos.getChannel()) {

            long size = sourceChannel.size();
            long transferred = 0;
            while (transferred < size) {
                transferred += sourceChannel.transferTo(0, size, targetChannel);
            }
        }
    }



}
